"""
管理后台 - Banner管理接口
"""
from typing import Optional
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.ext.asyncio import AsyncSession

from app.dependencies import get_db, get_current_user
from app.schemas.common import ResponseModel, PaginatedResponse
from app.schemas.content import BannerCreate, BannerUpdate, BannerResponse
from app.crud.base import CRUDBase
from app.models.content import Banner


router = APIRouter()
crud_banner = CRUDBase[Banner, BannerCreate, BannerUpdate](Banner)


@router.get("", response_model=ResponseModel)
async def get_banners(
    page: int = Query(1, ge=1, description="页码"),
    page_size: int = Query(10, ge=1, le=100, description="每页数量"),
    status: Optional[int] = Query(None, description="状态"),
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    获取Banner列表
    """
    filters = {}
    if status is not None:
        filters["status"] = status
    
    skip = (page - 1) * page_size
    banners = await crud_banner.get_multi(db, skip=skip, limit=page_size, filters=filters)
    total = await crud_banner.get_count(db, filters=filters)
    
    return ResponseModel(
        data=PaginatedResponse(
            total=total,
            page=page,
            page_size=page_size,
            items=[BannerResponse.model_validate(b) for b in banners]
        )
    )


@router.get("/{banner_id}", response_model=ResponseModel)
async def get_banner(
    banner_id: int,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    获取Banner详情
    """
    banner = await crud_banner.get_by_id(db, id=banner_id)
    if not banner:
        raise HTTPException(status_code=404, detail="Banner不存在")
    
    return ResponseModel(data=BannerResponse.model_validate(banner))


@router.post("", response_model=ResponseModel)
async def create_banner(
    banner_in: BannerCreate,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    创建Banner
    """
    banner = await crud_banner.create(db, obj_in=banner_in)
    return ResponseModel(
        code=201,
        message="创建成功",
        data=BannerResponse.model_validate(banner)
    )


@router.put("/{banner_id}", response_model=ResponseModel)
async def update_banner(
    banner_id: int,
    banner_in: BannerUpdate,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    更新Banner
    """
    banner = await crud_banner.get_by_id(db, id=banner_id)
    if not banner:
        raise HTTPException(status_code=404, detail="Banner不存在")
    
    banner = await crud_banner.update(db, db_obj=banner, obj_in=banner_in)
    return ResponseModel(
        message="更新成功",
        data=BannerResponse.model_validate(banner)
    )


@router.delete("/{banner_id}", response_model=ResponseModel)
async def delete_banner(
    banner_id: int,
    db: AsyncSession = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """
    删除Banner
    """
    success = await crud_banner.delete(db, id=banner_id)
    if not success:
        raise HTTPException(status_code=404, detail="Banner不存在")
    
    return ResponseModel(message="删除成功")

